<?php


namespace app\admin\model;


use think\Model;

use think\Db;

class ProductCateModel extends Model
{
    protected $name = 'product_cate';
    protected $field=true;

//    protected $autoWriteTimestamp = true;   // 开启自动写入时间戳

    /**

     * [getAllCount 根据条件获取所有配置信息数量]

     * @param $map

     * @return int|string

     */

    public function getAllCount($map)

    {

        return $this->where($map)->count();

    }

    /**

     * [getAllCate 获取全部分类]

     * @param $map

     * @param $Nowpage

     * @param $limits

     * @param $od

     * @return bool|\PDOStatement|string|\think\Collection

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function getAllCate($map, $Nowpage, $limits,$od)

    {

        return $this->where($map)->page($Nowpage, $limits)->order($od)->select();

    }

    /**
     * 添加
     * @param $param
     * @return array
     */

    public function insertTo($param)

    {

        Db::startTrans();// 启动事务

        try{

            $this->allowField(true)->save($param);

            Db::commit();// 提交事务

            writelog('【'.$param['name'].'】添加成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '添加成功'];

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('【'.$param['name'].'】添加失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' =>'添加失败'];

        }

    }

    public function getOneCate($id)

    {

        return $this->where('id', $id)->find();

    }

    public function editCate($param)

    {

        Db::startTrans();// 启动事务

        try{

            $this->save($param, ['id' => $param['id']]);


            Db::commit();// 提交事务

            writelog('产品分类【'.$param['name'].'】编辑成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '分类编辑成功'];

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('产品分类【'.$param['name'].'】编辑失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '分类编辑失败'];

        }

    }

    public function delCate($id)

    {

        $title = $this->where('id',$id)->value('name');

        Db::startTrans();// 启动事务

        try{

            $this->where('id', $id)->delete();

            Db::commit();// 提交事务

            writelog('【'.$title.'】删除成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '删除成功'];

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('【'.$title.'】删除失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' =>  '删除失败'];

        }

    }

    public function cateState($id,$num)

    {

        $name = Db::name('product_cate')->where(['id'=>$id])->value('name');//判断当前状态情况

        if($num == 2){

            $msg = '禁用';

        }else{

            $msg = '启用';

        }

        Db::startTrans();// 启动事务

        try {

            $this->where ('id' , $id)->setField (['status' => $num]);

            Db::commit();// 提交事务

            writelog ( '【' . $name . '】'.$msg.'成功' , 200);

            return ['code' => 200 , 'data' => '' , 'msg' => '已'.$msg];

        } catch (\Exception $e) {

            Db::rollback();// 回滚事务

            writelog ( '【' . $name . '】'.$msg.'失败' , 100);

            return ['code' => 100 , 'data' => $e->getMessage(), 'msg' => $msg.'失败'];

        }

    }
    //批量删除
    public function batchDel($param){

        Db::startTrans();// 启动事务

        try{

            self::destroy($param);

            Db::commit();// 提交事务

            writelog('批量删除成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '批量删除成功'];

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('批量删除失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量删除失败'];

        }

    }
    //批量禁用
    public function forbiddenCate($param){

        Db::startTrans();// 启动事务

        try{

            if($param){

                $this->saveAll($param);

            }else{

                $this->where('1=1')->update(['status'=>2]);

            }

            Db::commit();// 提交事务

            writelog('批量禁用分类成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '批量禁用成功'];

        }catch( \Exception $e){

            Db::rollback ();//回滚事务

            writelog('批量禁用分类失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量禁用失败'];

        }

    }

    //批量启用
    public function usingCate($param){

        Db::startTrans();// 启动事务

        try{

            if($param){

                $this->saveAll($param);

            }else{

                $this->where('1=1')->update(['status'=>1]);

            }

            Db::commit();// 提交事务

            writelog('批量启用分类成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '批量启用成功'];

        }catch( \Exception $e){

            Db::rollback ();//回滚事务

            writelog('批量启用分类失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量启用失败'];

        }

    }

    //获取列表
    public function getCate()

    {

        return $this->where(array('status'=>1),)->order('sort ')->select();

    }
}